package com.atwisdom.star.service.util;

import cn.hutool.core.collection.CollectionUtil;
import com.atwisdom.star.core.dao.pojo.modelClass.hier.HierInstance;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName: InstanceDataUtil
 * @ Description:
 * @author:王红艳
 * @date:2024/05/31 13:00
 */
public class InstanceDataUtil {

    /**
     * 筛选node下的所有hier数据
     * @param hierInstancesList 所有hier数据
     * @param nodeId 节点id
     * @param filterList 筛选后的hier数据
     */
    public static void getChildrenByNodeId(List<HierInstance> hierInstancesList,String nodeId,List<HierInstance> filterList){
        List<HierInstance> list = hierInstancesList.stream()
                .filter(hierInstance -> nodeId.equals(hierInstance.getParentId()))
                .collect(Collectors.toList());
        filterList.addAll(list);
        for(HierInstance hierInstance:list){
            getChildrenByNodeId(hierInstancesList,hierInstance.getNodeId(),filterList);
        }
    }

    /**
     * 筛选node下的所有hier数据,包括自己
     * @param hierInstancesList 所有hier数据
     * @param nodeId 节点id
     */
    public static List<HierInstance> getChildrenAndMeByNodeId(List<HierInstance> hierInstancesList,String nodeId){
        List<HierInstance>  filterList = hierInstancesList.stream()
                .filter(hierInstance -> nodeId.equals(hierInstance.getNodeId()))
                .collect(Collectors.toList());
        getChildrenByNodeId(hierInstancesList,nodeId,filterList);
        return filterList;
    }

    /**
     * 获取nodeId节点数据
     * @param hierInstancesList 所有hier数据
     * @param nodeId 节点id
     */
    public static HierInstance getHierInstanceByNodeId(List<HierInstance> hierInstancesList,String nodeId){
        List<HierInstance>  filterList = hierInstancesList.stream()
                .filter(hierInstance -> nodeId.equals(hierInstance.getNodeId()))
                .collect(Collectors.toList());
        return CollectionUtil.isEmpty(filterList)?null:filterList.get(0);
    }

    /**
     * 获取所有hier关联的linkId
     * @param hierInstancesList
     * @return
     */
    public static List<Long> getLinkIdsByHierList(List<HierInstance> hierInstancesList){
        return hierInstancesList.stream()
                .filter(hierInstance -> null!=hierInstance.getLinkId())
                .map(hierInstance -> hierInstance.getLinkId())
                .collect(Collectors.toList());
    }

    /**
     * 获取所有hier的id
     * @param hierInstancesList
     * @return
     */
    public static List<Long> getIdsByHierList(List<HierInstance> hierInstancesList){
        return hierInstancesList.stream()
                .filter(hierInstance -> null!=hierInstance.getId())
                .map(hierInstance -> hierInstance.getId())
                .collect(Collectors.toList());
    }

    /**
     * List<Long>转为List<String>
     * @param list 要转换的数据
     * @return 转换后的数据
     */
    public static List<String> listLongToListString(List<Long> list){
        return list.stream()
                .map(Object::toString)
                .collect(Collectors.toList());
    }

    /**
     * 使用in查询条件时，拼接in后的数据
     *
     * @Param [condition, dataList]
     * @Author gsh
     * @Date 2023/6/30
     **/
    public static String condJoin(StringBuffer condition, List<String> dataList, Set<String> keyList) {
        if (null != dataList) {
            for (String batchNo : dataList) {
                condition.append("'").append(batchNo).append("',");
            }
        } else if (null != keyList) {
            for (String batchNo : keyList) {
                condition.append("'").append(batchNo).append("',");
            }
        }
        return condition.deleteCharAt(condition.length() - 1).append(")").toString();
    }
}
